<?php
/**
 * System item Controller
 *
 *
 * @version $Id: ItemController.php 77 2010-04-25 07:28:48Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Controller_Action
 * @package admin
 * @subpackage Controller
 */
class Admin_ItemController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {

    }

    public function listAction()
    {
        $root = $this->getRequest()->getParam('root');
        $system_text = new  Default_Model_SystemText();
        if ($root == 'source')
        $arr = $system_text->getRoot();
        else
        $arr = array();
        $this->_helper->json($arr);
    }

    public function setAction()
    {
        $sn = (int)$this->getRequest()->getParam('sn', 0);
        $this->getHelper('layout')->disableLayout();
        $systemText = new Default_Model_SystemText();
        $currentData = $systemText->getTable()->find($sn);
        $this->view->name = $currentData->name;
        $this->view->parent_sn = $currentData->sn;
        $this->view->kind = $currentData->kind;
        $this->view->data = $systemText->getChildrens($sn);
    }

    public function setallAction()
    {
        $id = $this->getRequest()->getParam('id');
        $sort = $this->getRequest()->getParam('sort');
        $name = $this->getRequest()->getParam('name');
        $filter = new Zend_Filter_StripTags();
        foreach ($id as $sn=>$val) {
            if ($val) {
                $systemText = new Default_Model_SystemText();
                $systemText->assignIdentifier($sn);
                $systemText->id = $filter->filter($val);
                $systemText->sort = (int)$sort[$sn];
                $systemText->name = $filter->filter($name[$sn]);
                $systemText->save();
            }
        }
        $parent_sn = $this->getRequest()->getParam('parent_sn');
        $kind = $this->getRequest()->getParam('kind');
        // 新增
        $ids = $this->getRequest()->getParam('ids');
        if ($ids) {
            $sorts = $this->getRequest()->getParam('sorts');
            $names = $this->getRequest()->getParam('names');
            foreach ($ids as $id =>$val) {
                $res = Doctrine_Query::create()
                ->select('id')
                ->from('Default_Model_SystemText')
                ->where('kind=?', $kind)
                ->andWhere('id=?', $val)
                ->andWhere('parent_sn=?',$parent_sn)
                ->fetchOne();
                if (!$res) {
                    $systemText = new Default_Model_SystemText();
                    $systemText->sort = $sorts[$id];
                    $systemText->kind = $kind;
                    $systemText->parent_sn = $parent_sn;
                    $systemText->name = $filter->filter($names[$id]);
                    $systemText->id = $filter->filter($val);
                    $systemText->save();
                }
            }
        }

        $this->_helper->json(1);
    }


    public function deleteAction()
    {
        $sn = (int) $this->getRequest()->getParam('sn');

        $db = new Default_Model_SystemText();
        $db->assignIdentifier($sn);
        $db->delete();
        $this->_helper->json(1);
    }
}

